##############
###### STUDY 1, yougov
##############
library(readr)
library(huxtable)
library(interactions)
library(gridExtra)
library(dplyr)
library(ggplot2)
library(patchwork)

study1_PNAS_yougov <- read_csv("study1_PNAS_white_only.csv")
study1_PNAS_yougov$Diversification[study1_PNAS_yougov$Q18treat=="Hide"]=0
study1_PNAS_yougov$Diversification[study1_PNAS_yougov$Q18treat=="Show"]=1

study1_PNAS_yougov$VOTER_REGISTERED_ALL[study1_PNAS_yougov$votereg=="Yes"]=1
study1_PNAS_yougov$VOTER_REGISTERED_ALL[study1_PNAS_yougov$votereg=="No"]=0
study1_PNAS_yougov$VOTER_REGISTERED_ALL[study1_PNAS_yougov$votereg=="Don't know"]=0

study1_PNAS_yougov$GENDER[study1_PNAS_yougov$gender=="Male"]=0
study1_PNAS_yougov$GENDER[study1_PNAS_yougov$gender=="Female"]=1

study1_PNAS_yougov$EDU[study1_PNAS_yougov$educ=="No HS"]=0
study1_PNAS_yougov$EDU[study1_PNAS_yougov$educ=="High school graduate"]=1/5
study1_PNAS_yougov$EDU[study1_PNAS_yougov$educ=="Some college"]=2/5
study1_PNAS_yougov$EDU[study1_PNAS_yougov$educ=="2-year"]=3/5
study1_PNAS_yougov$EDU[study1_PNAS_yougov$educ=="4-year"]=4/5
study1_PNAS_yougov$EDU[study1_PNAS_yougov$educ=="Post-grad"]=1

study1_PNAS_yougov$PID[study1_PNAS_yougov$pid7=="Strong Democrat"]=0
study1_PNAS_yougov$PID[study1_PNAS_yougov$pid7=="Not very strong Democrat"]=1/6
study1_PNAS_yougov$PID[study1_PNAS_yougov$pid7=="Lean Democrat"]=2/6
study1_PNAS_yougov$PID[study1_PNAS_yougov$pid7=="Independent"]=3/6
study1_PNAS_yougov$PID[study1_PNAS_yougov$pid7=="Lean Republican"]=4/6
study1_PNAS_yougov$PID[study1_PNAS_yougov$pid7=="Not very strong Republican"]=5/6
study1_PNAS_yougov$PID[study1_PNAS_yougov$pid7=="Strong Republican"]=1

study1_PNAS_yougov$Blacks_less_than_they_deserve[study1_PNAS_yougov$racialresent_1=="Strongly disagree"]=0
study1_PNAS_yougov$Blacks_less_than_they_deserve[study1_PNAS_yougov$racialresent_1=="Disagree"]=1/4
study1_PNAS_yougov$Blacks_less_than_they_deserve[study1_PNAS_yougov$racialresent_1=="Neither agree nor disagree"]=2/4
study1_PNAS_yougov$Blacks_less_than_they_deserve[study1_PNAS_yougov$racialresent_1=="Agree"]=3/4
study1_PNAS_yougov$Blacks_less_than_they_deserve[study1_PNAS_yougov$racialresent_1=="Strongly agree"]=1

study1_PNAS_yougov$Blacks_less_than_they_deserve_reverse_coded[study1_PNAS_yougov$racialresent_1=="Strongly disagree"]=1
study1_PNAS_yougov$Blacks_less_than_they_deserve_reverse_coded[study1_PNAS_yougov$racialresent_1=="Disagree"]=3/4
study1_PNAS_yougov$Blacks_less_than_they_deserve_reverse_coded[study1_PNAS_yougov$racialresent_1=="Neither agree nor disagree"]=2/4
study1_PNAS_yougov$Blacks_less_than_they_deserve_reverse_coded[study1_PNAS_yougov$racialresent_1=="Agree"]=1/4
study1_PNAS_yougov$Blacks_less_than_they_deserve_reverse_coded[study1_PNAS_yougov$racialresent_1=="Strongly agree"]=0

study1_PNAS_yougov$Blacks_overcome_without_favors[study1_PNAS_yougov$racialresent_2=="Strongly disagree"]=0
study1_PNAS_yougov$Blacks_overcome_without_favors[study1_PNAS_yougov$racialresent_2=="Disagree"]=1/4
study1_PNAS_yougov$Blacks_overcome_without_favors[study1_PNAS_yougov$racialresent_2=="Neither agree nor disagree"]=2/4
study1_PNAS_yougov$Blacks_overcome_without_favors[study1_PNAS_yougov$racialresent_2=="Agree"]=3/4
study1_PNAS_yougov$Blacks_overcome_without_favors[study1_PNAS_yougov$racialresent_2=="Strongly agree"]=1

study1_PNAS_yougov$Black_not_trying_hard_enough[study1_PNAS_yougov$racialresent_3=="Strongly disagree"]=0
study1_PNAS_yougov$Black_not_trying_hard_enough[study1_PNAS_yougov$racialresent_3=="Disagree"]=1/4
study1_PNAS_yougov$Black_not_trying_hard_enough[study1_PNAS_yougov$racialresent_3=="Neither agree nor disagree"]=2/4
study1_PNAS_yougov$Black_not_trying_hard_enough[study1_PNAS_yougov$racialresent_3=="Agree"]=3/4
study1_PNAS_yougov$Black_not_trying_hard_enough[study1_PNAS_yougov$racialresent_3=="Strongly agree"]=1

study1_PNAS_yougov$generation_of_slavery_made_hard[study1_PNAS_yougov$racialresent_4=="Strongly disagree"]=0
study1_PNAS_yougov$generation_of_slavery_made_hard[study1_PNAS_yougov$racialresent_4=="Disagree"]=1/4
study1_PNAS_yougov$generation_of_slavery_made_hard[study1_PNAS_yougov$racialresent_4=="Neither agree nor disagree"]=2/4
study1_PNAS_yougov$generation_of_slavery_made_hard[study1_PNAS_yougov$racialresent_4=="Agree"]=3/4
study1_PNAS_yougov$generation_of_slavery_made_hard[study1_PNAS_yougov$racialresent_4=="Strongly agree"]=1

study1_PNAS_yougov$generation_of_slavery_made_hard_reverse_coded[study1_PNAS_yougov$racialresent_4=="Strongly disagree"]=1
study1_PNAS_yougov$generation_of_slavery_made_hard_reverse_coded[study1_PNAS_yougov$racialresent_4=="Disagree"]=2/4
study1_PNAS_yougov$generation_of_slavery_made_hard_reverse_coded[study1_PNAS_yougov$racialresent_4=="Neither agree nor disagree"]=2/4
study1_PNAS_yougov$generation_of_slavery_made_hard_reverse_coded[study1_PNAS_yougov$racialresent_4=="Agree"]=1/4
study1_PNAS_yougov$generation_of_slavery_made_hard_reverse_coded[study1_PNAS_yougov$racialresent_4=="Strongly agree"]=0

study1_PNAS_yougov$INCOME[study1_PNAS_yougov$faminc_new == "Less than $10,000"]=0
study1_PNAS_yougov$INCOME[study1_PNAS_yougov$faminc_new =="$10,000 - $19,999"]=1/15
study1_PNAS_yougov$INCOME[study1_PNAS_yougov$faminc_new =="$20,000 - $29,999"]=2/15
study1_PNAS_yougov$INCOME[study1_PNAS_yougov$faminc_new =="$30,000 - $39,999"]=3/15
study1_PNAS_yougov$INCOME[study1_PNAS_yougov$faminc_new =="$40,000 - $49,999"]=4/15
study1_PNAS_yougov$INCOME[study1_PNAS_yougov$faminc_new =="$50,000 - $59,999"]=5/15
study1_PNAS_yougov$INCOME[study1_PNAS_yougov$faminc_new =="$60,000 - $69,999"]=6/15
study1_PNAS_yougov$INCOME[study1_PNAS_yougov$faminc_new =="$70,000 - $79,999"]=7/15
study1_PNAS_yougov$INCOME[study1_PNAS_yougov$faminc_new =="$80,000 - $99,999"]=8/15
study1_PNAS_yougov$INCOME[study1_PNAS_yougov$faminc_new =="$100,000 - $119,999"]=9/15
study1_PNAS_yougov$INCOME[study1_PNAS_yougov$faminc_new =="$120,000 - $149,999"]=10/15
study1_PNAS_yougov$INCOME[study1_PNAS_yougov$faminc_new =="$150,000 - $199,999"]=11/15
study1_PNAS_yougov$INCOME[study1_PNAS_yougov$faminc_new =="$200,000 - $249,999"]=12/15
study1_PNAS_yougov$INCOME[study1_PNAS_yougov$faminc_new =="$250,000 - $349,999"]=13/15
study1_PNAS_yougov$INCOME[study1_PNAS_yougov$faminc_new =="$350,000 - $499,999"]=14/15
study1_PNAS_yougov$INCOME[study1_PNAS_yougov$faminc_new =="$500,000 or more"]=1

study1_PNAS_yougov$democratic_Party_FT = as.numeric(study1_PNAS_yougov$Q36)
study1_PNAS_yougov$republican_Party_FT = as.numeric(study1_PNAS_yougov$Q37)

study1_PNAS_yougov$Republican_Party_FT = (study1_PNAS_yougov$republican_Party_FT)/100
study1_PNAS_yougov$Democratic_Party_FT = (study1_PNAS_yougov$democratic_Party_FT)/100

study1_PNAS_yougov$Affective_Polarization = abs(study1_PNAS_yougov$Democratic_Party_FT - study1_PNAS_yougov$Republican_Party_FT)

study1_PNAS_yougov$Support_for_political_violence[study1_PNAS_yougov$Q38_3=="Very strongly disagree"]=0
study1_PNAS_yougov$Support_for_political_violence[study1_PNAS_yougov$Q38_3=="Strongly disagree"]=1/5
study1_PNAS_yougov$Support_for_political_violence[study1_PNAS_yougov$Q38_3=="Disagree"]=2/5
study1_PNAS_yougov$Support_for_political_violence[study1_PNAS_yougov$Q38_3=="Agree"]=3/5
study1_PNAS_yougov$Support_for_political_violence[study1_PNAS_yougov$Q38_3=="Strongly agree"]=4/5
study1_PNAS_yougov$Support_for_political_violence[study1_PNAS_yougov$Q38_3=="Very strongly agree"]=1

study1_PNAS_yougov$Racial_resentment_scale = (study1_PNAS_yougov$Blacks_less_than_they_deserve_reverse_coded + 
                                                     study1_PNAS_yougov$Black_not_trying_hard_enough +
                                                     study1_PNAS_yougov$generation_of_slavery_made_hard_reverse_coded +
                                                     study1_PNAS_yougov$Blacks_overcome_without_favors)/4
study1_PNAS_yougov$AGE =  2023-(study1_PNAS_yougov$birthyr)

study1_PNAS_yougov$IDEO[study1_PNAS_yougov$ideo5=="Very liberal"]=0
study1_PNAS_yougov$IDEO[study1_PNAS_yougov$ideo5=="Liberal"]=1/4
study1_PNAS_yougov$IDEO[study1_PNAS_yougov$ideo5=="Moderate"]=2/4
study1_PNAS_yougov$IDEO[study1_PNAS_yougov$ideo5=="Conservative"]=3/4
study1_PNAS_yougov$IDEO[study1_PNAS_yougov$ideo5=="Very conservative"]=1

study1_PNAS_yougov$BlackΔ = (study1_PNAS_yougov$county_change_in_pct_black)
study1_PNAS_yougov$WhiteΔ = (study1_PNAS_yougov$county_change_in_pct_white)

study1_PNAS_yougov_WHITE<- dplyr::select(filter(study1_PNAS_yougov, race == "White"), c(caseid:WhiteΔ))
#write_csv(study1_PNAS_yougov_WHITE_Black_increase_only_treatment, 
#         file = "yougov_diversification_Black_increase_only.csv")


#########################################
#########################################
######## STUDY 2 cleaning
#########################################
#########################################
study2_PNAS_LUCID <- read_tsv("~/Google Drive/My Drive/Fall_2023/ongoing_projects/white_fight/lucid_chronic_threat_with_pop_change.tsv")

# write_excel_csv(study2_PNAS_LUCID_white, file = "white_fight_lucid.csv")

study2_PNAS_LUCID$diversification_prime[study2_PNAS_LUCID$random1==0]=0
study2_PNAS_LUCID$diversification_prime[study2_PNAS_LUCID$random1==1]=1

study2_PNAS_LUCID$GENDER[study2_PNAS_LUCID$gender=="Male"]=0
study2_PNAS_LUCID$GENDER[study2_PNAS_LUCID$gender=="Female"]=1

study2_PNAS_LUCID$PID[study2_PNAS_LUCID$pid=="Strong Democrat"]=0
study2_PNAS_LUCID$PID[study2_PNAS_LUCID$pid=="Not so strong Democrat"]=1/6
study2_PNAS_LUCID$PID[study2_PNAS_LUCID$pid=="Independent, leaning Democrat"]=2/6
study2_PNAS_LUCID$PID[study2_PNAS_LUCID$pid=="Independent"]=3/6
study2_PNAS_LUCID$PID[study2_PNAS_LUCID$pid=="Independent, leaning Republican"]=4/6
study2_PNAS_LUCID$PID[study2_PNAS_LUCID$pid=="Not so strong Republican"]=5/6
study2_PNAS_LUCID$PID[study2_PNAS_LUCID$pid=="Strong Republican"]=1

study2_PNAS_LUCID$PID_post[study2_PNAS_LUCID$pid_post=="Strong Democrat"]=0
study2_PNAS_LUCID$PID_post[study2_PNAS_LUCID$pid_post=="Not so strong Democrat"]=1/6
study2_PNAS_LUCID$PID_post[study2_PNAS_LUCID$pid_post=="Independent, leaning Democrat"]=2/6
study2_PNAS_LUCID$PID_post[study2_PNAS_LUCID$pid_post=="Independent"]=3/6
study2_PNAS_LUCID$PID_post[study2_PNAS_LUCID$pid_post=="Independent, leaning Republican"]=4/6
study2_PNAS_LUCID$PID_post[study2_PNAS_LUCID$pid_post=="Not so strong Republican"]=5/6
study2_PNAS_LUCID$PID_post[study2_PNAS_LUCID$pid_post=="Strong Republican"]=1

study2_PNAS_LUCID$IDEO_pre[study2_PNAS_LUCID$ideo=="Extremely liberal"]=0
study2_PNAS_LUCID$IDEO_pre[study2_PNAS_LUCID$ideo=="Liberal"]=1/6
study2_PNAS_LUCID$IDEO_pre[study2_PNAS_LUCID$ideo=="Slightly liberal"]=2/6
study2_PNAS_LUCID$IDEO_pre[study2_PNAS_LUCID$ideo=="Moderate, middle of the road"]=3/6
study2_PNAS_LUCID$IDEO_pre[study2_PNAS_LUCID$ideo=="Slightly conservative"]=4/6
study2_PNAS_LUCID$IDEO_pre[study2_PNAS_LUCID$ideo=="Conservative"]=5/6
study2_PNAS_LUCID$IDEO_pre[study2_PNAS_LUCID$ideo=="Extremely conservative"]=1

study2_PNAS_LUCID$IDEO_post[study2_PNAS_LUCID$ideo_post=="Extremely liberal"]=0
study2_PNAS_LUCID$IDEO_post[study2_PNAS_LUCID$ideo_post=="Liberal"]=1/6
study2_PNAS_LUCID$IDEO_post[study2_PNAS_LUCID$ideo_post=="Slightly liberal"]=2/6
study2_PNAS_LUCID$IDEO_post[study2_PNAS_LUCID$ideo_post=="Moderate, middle of the road"]=3/6
study2_PNAS_LUCID$IDEO_post[study2_PNAS_LUCID$ideo_post=="Slightly conservative"]=4/6
study2_PNAS_LUCID$IDEO_post[study2_PNAS_LUCID$ideo_post=="Conservative"]=5/6
study2_PNAS_LUCID$IDEO_post[study2_PNAS_LUCID$ideo_post=="Extremely conservative"]=1


study2_PNAS_LUCID$INCOME[study2_PNAS_LUCID$income=="Less than $10,000"]=0
study2_PNAS_LUCID$INCOME[study2_PNAS_LUCID$income=="$10,000 to $19,999"]=1/11
study2_PNAS_LUCID$INCOME[study2_PNAS_LUCID$income=="$20,000 to $29,999"]=2/11
study2_PNAS_LUCID$INCOME[study2_PNAS_LUCID$income=="$30,000 to $39,999"]=3/11
study2_PNAS_LUCID$INCOME[study2_PNAS_LUCID$income=="$40,000 to $49,999"]=4/11
study2_PNAS_LUCID$INCOME[study2_PNAS_LUCID$income=="$50,000 to $59,999"]=5/11
study2_PNAS_LUCID$INCOME[study2_PNAS_LUCID$income=="$60,000 to $69,999"]=6/11
study2_PNAS_LUCID$INCOME[study2_PNAS_LUCID$income=="$70,000 to $79,999"]=7/11
study2_PNAS_LUCID$INCOME[study2_PNAS_LUCID$income=="$80,000 to $89,999"]=8/11
study2_PNAS_LUCID$INCOME[study2_PNAS_LUCID$income=="$90,000 to $99,999"]=9/11
study2_PNAS_LUCID$INCOME[study2_PNAS_LUCID$income=="$100,000 to $149,999"]=10/11
study2_PNAS_LUCID$INCOME[study2_PNAS_LUCID$income=="$150,000 or more"]=1

study2_PNAS_LUCID$EDU[study2_PNAS_LUCID$edu=="Less than high school degree"]=0
study2_PNAS_LUCID$EDU[study2_PNAS_LUCID$edu=="High school graduate (high school diploma or equivalent including GED)"]=1/6
study2_PNAS_LUCID$EDU[study2_PNAS_LUCID$edu=="Some college but no degree"]=2/6
study2_PNAS_LUCID$EDU[study2_PNAS_LUCID$edu=="Associate degree in college (2-year)"]=3/6
study2_PNAS_LUCID$EDU[study2_PNAS_LUCID$edu=="Bachelor's degree in college (4-year)"]=4/6
study2_PNAS_LUCID$EDU[study2_PNAS_LUCID$edu=="Master's degree"]=5/6
study2_PNAS_LUCID$EDU[study2_PNAS_LUCID$edu=="Doctoral degree"]=1
study2_PNAS_LUCID$EDU[study2_PNAS_LUCID$edu=="Professional degree (JD, MD)"]=1



study2_PNAS_LUCID$RACE_import_pre[study2_PNAS_LUCID$race_iden_pre=="Not at all important"]=0
study2_PNAS_LUCID$RACE_import_pre[study2_PNAS_LUCID$race_iden_pre=="Not very important"]=1/4
study2_PNAS_LUCID$RACE_import_pre[study2_PNAS_LUCID$race_iden_pre=="Somewhat important"]=2/4
study2_PNAS_LUCID$RACE_import_pre[study2_PNAS_LUCID$race_iden_pre=="Very important"]=3/4
study2_PNAS_LUCID$RACE_import_pre[study2_PNAS_LUCID$race_iden_pre=="Extremely important"]=1

study2_PNAS_LUCID$LINKED_fate_pre[study2_PNAS_LUCID$linked_fate_pre=="Not at all"]=0
study2_PNAS_LUCID$LINKED_fate_pre[study2_PNAS_LUCID$linked_fate_pre=="Not very much"]=1/3
study2_PNAS_LUCID$LINKED_fate_pre[study2_PNAS_LUCID$linked_fate_pre=="Some"]=2/3
study2_PNAS_LUCID$LINKED_fate_pre[study2_PNAS_LUCID$linked_fate_pre=="A lot"]=1

study2_PNAS_LUCID$RACE_import_post[study2_PNAS_LUCID$race_ident2=="Not at all important"]=0
study2_PNAS_LUCID$RACE_import_post[study2_PNAS_LUCID$race_ident2=="Not very important"]=1/4
study2_PNAS_LUCID$RACE_import_post[study2_PNAS_LUCID$race_ident2=="Somewhat important"]=2/4
study2_PNAS_LUCID$RACE_import_post[study2_PNAS_LUCID$race_ident2=="Very important"]=3/4
study2_PNAS_LUCID$RACE_import_post[study2_PNAS_LUCID$race_ident2=="Extremely important"]=1

study2_PNAS_LUCID$LINKED_fate_post[study2_PNAS_LUCID$linked_fate2=="Not at all"]=0
study2_PNAS_LUCID$LINKED_fate_post[study2_PNAS_LUCID$linked_fate2=="Not very much"]=1/3
study2_PNAS_LUCID$LINKED_fate_post[study2_PNAS_LUCID$linked_fate2=="Some"]=2/3
study2_PNAS_LUCID$LINKED_fate_post[study2_PNAS_LUCID$linked_fate2=="A lot"]=1

study2_PNAS_LUCID$national_demographic_change[study2_PNAS_LUCID$demo_national_pre=="Very hopeful"]=0
study2_PNAS_LUCID$national_demographic_change[study2_PNAS_LUCID$demo_national_pre=="Hopeful"]=1/6
study2_PNAS_LUCID$national_demographic_change[study2_PNAS_LUCID$demo_national_pre=="Slightly hopeful"]=2/6
study2_PNAS_LUCID$national_demographic_change[study2_PNAS_LUCID$demo_national_pre=="Neither hopeful, nor fearful"]=3/6
study2_PNAS_LUCID$national_demographic_change[study2_PNAS_LUCID$demo_national_pre=="Slightly fearful"]=4/6
study2_PNAS_LUCID$national_demographic_change[study2_PNAS_LUCID$demo_national_pre=="Fearful"]=5/6
study2_PNAS_LUCID$national_demographic_change[study2_PNAS_LUCID$demo_national_pre=="Very fearful"]=1

study2_PNAS_LUCID$national_demographic_change_post[study2_PNAS_LUCID$demo_national_post=="Very hopeful"]=0
study2_PNAS_LUCID$national_demographic_change_post[study2_PNAS_LUCID$demo_national_post=="Hopeful"]=1/6
study2_PNAS_LUCID$national_demographic_change_post[study2_PNAS_LUCID$demo_national_post=="Slightly hopeful"]=2/6
study2_PNAS_LUCID$national_demographic_change_post[study2_PNAS_LUCID$demo_national_post=="Neither hopeful, nor fearful"]=3/6
study2_PNAS_LUCID$national_demographic_change_post[study2_PNAS_LUCID$demo_national_post=="Slightly fearful"]=4/6
study2_PNAS_LUCID$national_demographic_change_post[study2_PNAS_LUCID$demo_national_post=="Fearful"]=5/6
study2_PNAS_LUCID$national_demographic_change_post[study2_PNAS_LUCID$demo_national_post=="Very fearful"]=1

study2_PNAS_LUCID$local_demographic_change[study2_PNAS_LUCID$demo_local_pre=="Very hopeful"]=0
study2_PNAS_LUCID$local_demographic_change[study2_PNAS_LUCID$demo_local_pre=="Hopeful"]=1/6
study2_PNAS_LUCID$local_demographic_change[study2_PNAS_LUCID$demo_local_pre=="Slightly hopeful"]=2/6
study2_PNAS_LUCID$local_demographic_change[study2_PNAS_LUCID$demo_local_pre=="Neither hopeful, nor fearful"]=3/6
study2_PNAS_LUCID$local_demographic_change[study2_PNAS_LUCID$demo_local_pre=="Slightly fearful"]=4/6
study2_PNAS_LUCID$local_demographic_change[study2_PNAS_LUCID$demo_local_pre=="Fearful"]=5/6
study2_PNAS_LUCID$local_demographic_change[study2_PNAS_LUCID$demo_local_pre=="Very fearful"]=1

study2_PNAS_LUCID$local_demographic_change_post[study2_PNAS_LUCID$demo_local_post=="Very hopeful"]=0
study2_PNAS_LUCID$local_demographic_change_post[study2_PNAS_LUCID$demo_local_post=="Hopeful"]=1/6
study2_PNAS_LUCID$local_demographic_change_post[study2_PNAS_LUCID$demo_local_post=="Slightly hopeful"]=2/6
study2_PNAS_LUCID$local_demographic_change_post[study2_PNAS_LUCID$demo_local_post=="Neither hopeful, nor fearful"]=3/6
study2_PNAS_LUCID$local_demographic_change_post[study2_PNAS_LUCID$demo_local_post=="Slightly fearful"]=4/6
study2_PNAS_LUCID$local_demographic_change_post[study2_PNAS_LUCID$demo_local_post=="Fearful"]=5/6
study2_PNAS_LUCID$local_demographic_change_post[study2_PNAS_LUCID$demo_local_post=="Very fearful"]=1


study2_PNAS_LUCID$reasonable_VIOLENCE_pre[study2_PNAS_LUCID$anti_dem_pre_1=="Strongly disagree"]=0
study2_PNAS_LUCID$reasonable_VIOLENCE_pre[study2_PNAS_LUCID$anti_dem_pre_1=="Disagree"]=1/6
study2_PNAS_LUCID$reasonable_VIOLENCE_pre[study2_PNAS_LUCID$anti_dem_pre_1=="Somewhat disagree"]=2/6
study2_PNAS_LUCID$reasonable_VIOLENCE_pre[study2_PNAS_LUCID$anti_dem_pre_1=="Neither agree nor disagree"]=3/6
study2_PNAS_LUCID$reasonable_VIOLENCE_pre[study2_PNAS_LUCID$anti_dem_pre_1=="Somewhat agree"]=4/6
study2_PNAS_LUCID$reasonable_VIOLENCE_pre[study2_PNAS_LUCID$anti_dem_pre_1=="Agree"]=5/6
study2_PNAS_LUCID$reasonable_VIOLENCE_pre[study2_PNAS_LUCID$anti_dem_pre_1=="Strongly agree"]=1

study2_PNAS_LUCID$reasonable_VIOLENCE_post[study2_PNAS_LUCID$anti_dem_post_1=="Strongly disagree"]=0
study2_PNAS_LUCID$reasonable_VIOLENCE_post[study2_PNAS_LUCID$anti_dem_post_1=="Disagree"]=1/6
study2_PNAS_LUCID$reasonable_VIOLENCE_post[study2_PNAS_LUCID$anti_dem_post_1=="Somewhat disagree"]=2/6
study2_PNAS_LUCID$reasonable_VIOLENCE_post[study2_PNAS_LUCID$anti_dem_post_1=="Neither agree nor disagree"]=3/6
study2_PNAS_LUCID$reasonable_VIOLENCE_post[study2_PNAS_LUCID$anti_dem_post_1=="Somewhat agree"]=4/6
study2_PNAS_LUCID$reasonable_VIOLENCE_post[study2_PNAS_LUCID$anti_dem_post_1=="Agree"]=5/6
study2_PNAS_LUCID$reasonable_VIOLENCE_post[study2_PNAS_LUCID$anti_dem_post_1=="Strongly agree"]=1

study2_PNAS_LUCID$white_nationalism_THREAT[study2_PNAS_LUCID$white_nationalism=="Not a threat at all"]=0
study2_PNAS_LUCID$white_nationalism_THREAT[study2_PNAS_LUCID$white_nationalism=="Not a very serious threat"]=1/3
study2_PNAS_LUCID$white_nationalism_THREAT[study2_PNAS_LUCID$white_nationalism=="Somewhat serious threat"]=2/3
study2_PNAS_LUCID$white_nationalism_THREAT[study2_PNAS_LUCID$white_nationalism=="Very serious threat"]=1

study2_PNAS_LUCID$worse_if_a_rep_was_MURDERED[study2_PNAS_LUCID$par_schaden_pre=="Worse if they were Democratic"]=0
study2_PNAS_LUCID$worse_if_a_rep_was_MURDERED[study2_PNAS_LUCID$par_schaden_pre=="The same"]=0
study2_PNAS_LUCID$worse_if_a_rep_was_MURDERED[study2_PNAS_LUCID$par_schaden_pre=="Worse if they were Republican"]=1

study2_PNAS_LUCID$black_FUTURE_threat_pre[study2_PNAS_LUCID$future_threat_pre_1=="Not a threat at all"]=0
study2_PNAS_LUCID$black_FUTURE_threat_pre[study2_PNAS_LUCID$future_threat_pre_1=="Not a very serious threat"]=1/3
study2_PNAS_LUCID$black_FUTURE_threat_pre[study2_PNAS_LUCID$future_threat_pre_1=="Somewhat serious threat"]=2/3
study2_PNAS_LUCID$black_FUTURE_threat_pre[study2_PNAS_LUCID$future_threat_pre_1=="Very serious threat"]=1

study2_PNAS_LUCID$asian_FUTURE_threat_pre[study2_PNAS_LUCID$future_threat_pre_2=="Not a threat at all"]=0
study2_PNAS_LUCID$asian_FUTURE_threat_pre[study2_PNAS_LUCID$future_threat_pre_2=="Not a very serious threat"]=1/3
study2_PNAS_LUCID$asian_FUTURE_threat_pre[study2_PNAS_LUCID$future_threat_pre_2=="Somewhat serious threat"]=2/3
study2_PNAS_LUCID$asian_FUTURE_threat_pre[study2_PNAS_LUCID$future_threat_pre_2=="Very serious threat"]=1

study2_PNAS_LUCID$latinx_FUTURE_threat_pre[study2_PNAS_LUCID$future_threat_pre_3=="Not a threat at all"]=0
study2_PNAS_LUCID$latinx_FUTURE_threat_pre[study2_PNAS_LUCID$future_threat_pre_3=="Not a very serious threat"]=1/3
study2_PNAS_LUCID$latinx_FUTURE_threat_pre[study2_PNAS_LUCID$future_threat_pre_3=="Somewhat serious threat"]=2/3
study2_PNAS_LUCID$latinx_FUTURE_threat_pre[study2_PNAS_LUCID$future_threat_pre_3=="Very serious threat"]=1

study2_PNAS_LUCID$immigrants_FUTURE_threat_pre[study2_PNAS_LUCID$future_threat_pre_5=="Not a threat at all"]=0
study2_PNAS_LUCID$immigrants_FUTURE_threat_pre[study2_PNAS_LUCID$future_threat_pre_5=="Not a very serious threat"]=1/3
study2_PNAS_LUCID$immigrants_FUTURE_threat_pre[study2_PNAS_LUCID$future_threat_pre_5=="Somewhat serious threat"]=2/3
study2_PNAS_LUCID$immigrants_FUTURE_threat_pre[study2_PNAS_LUCID$future_threat_pre_5=="Very serious threat"]=1

study2_PNAS_LUCID_white<- dplyr::select(filter(study2_PNAS_LUCID, 
                                               race == "White"), 
                                        c(StartDate:local_demographic_change_post))


#########################################
#########################################
######## STUDY 3 cleaning
#########################################
#########################################

state_threat_white_change2$national_threat[state_threat_white_change2$random==0]=0
state_threat_white_change2$national_threat[state_threat_white_change2$random==1]=1

state_threat_white_change2$state_threat[state_threat_white_change2$random==0]=0
state_threat_white_change2$state_threat[state_threat_white_change2$random==2]=1

state_threat_white_change2$national_VS_state_threat[state_threat_white_change2$random==2]=0
state_threat_white_change2$national_VS_state_threat[state_threat_white_change2$random==1]=1

state_threat_white_change2$reasonable_violence_pre[state_threat_white_change2$anti_dem_pretreat_3=="Strongly disagree"]=0
state_threat_white_change2$reasonable_violence_pre[state_threat_white_change2$anti_dem_pretreat_3=="Somewhat disagree"]=1/4
state_threat_white_change2$reasonable_violence_pre[state_threat_white_change2$anti_dem_pretreat_3=="Neither agree nor disagree"]=2/4
state_threat_white_change2$reasonable_violence_pre[state_threat_white_change2$anti_dem_pretreat_3=="Somewhat agree"]=3/4
state_threat_white_change2$reasonable_violence_pre[state_threat_white_change2$anti_dem_pretreat_3=="Strongly agree"]=1


state_threat_white_change2$tradtional_disappearing_force_pre[state_threat_white_change2$anti_dem_pretreat_7=="Strongly disagree"]=0
state_threat_white_change2$tradtional_disappearing_force_pre[state_threat_white_change2$anti_dem_pretreat_7=="Somewhat disagree"]=1/4
state_threat_white_change2$tradtional_disappearing_force_pre[state_threat_white_change2$anti_dem_pretreat_7=="Neither agree nor disagree"]=2/4
state_threat_white_change2$tradtional_disappearing_force_pre[state_threat_white_change2$anti_dem_pretreat_7=="Somewhat agree"]=3/4
state_threat_white_change2$tradtional_disappearing_force_pre[state_threat_white_change2$anti_dem_pretreat_7=="Strongly agree"]=1

state_threat_white_change2$reasonable_violence_post[state_threat_white_change2$anti_dem_post_3=="Strongly disagree"]=0
state_threat_white_change2$reasonable_violence_post[state_threat_white_change2$anti_dem_post_3=="Somewhat disagree"]=1/4
state_threat_white_change2$reasonable_violence_post[state_threat_white_change2$anti_dem_post_3=="Neither agree nor disagree"]=2/4
state_threat_white_change2$reasonable_violence_post[state_threat_white_change2$anti_dem_post_3=="Somewhat agree"]=3/4
state_threat_white_change2$reasonable_violence_post[state_threat_white_change2$anti_dem_post_3=="Strongly agree"]=1

state_threat_white_change2$tradtional_disappearing_force_post[state_threat_white_change2$anti_dem_post_7=="Strongly disagree"]=0
state_threat_white_change2$tradtional_disappearing_force_post[state_threat_white_change2$anti_dem_post_7=="Somewhat disagree"]=1/4
state_threat_white_change2$tradtional_disappearing_force_post[state_threat_white_change2$anti_dem_post_7=="Neither agree nor disagree"]=2/4
state_threat_white_change2$tradtional_disappearing_force_post[state_threat_white_change2$anti_dem_post_7=="Somewhat agree"]=3/4
state_threat_white_change2$tradtional_disappearing_force_post[state_threat_white_change2$anti_dem_post_7=="Strongly agree"]=1


state_threat_white_change2$PID_pre[state_threat_white_change2$pid=="Strong Democrat"]=0
state_threat_white_change2$PID_pre[state_threat_white_change2$pid=="Not so strong Democrat"]=1/6
state_threat_white_change2$PID_pre[state_threat_white_change2$pid=="Independent, leaning Democrat"]=2/6
state_threat_white_change2$PID_pre[state_threat_white_change2$pid=="Independent"]=3/6
state_threat_white_change2$PID_pre[state_threat_white_change2$pid=="Independent, leaning Republican"]=4/6
state_threat_white_change2$PID_pre[state_threat_white_change2$pid=="Not so strong Republican"]=5/6
state_threat_white_change2$PID_pre[state_threat_white_change2$pid=="Strong Republican"]=1

state_threat_white_change2$PID_post[state_threat_white_change2$post_pid=="Strong Democrat"]=0
state_threat_white_change2$PID_post[state_threat_white_change2$post_pid=="Not so strong Democrat"]=1/6
state_threat_white_change2$PID_post[state_threat_white_change2$post_pid=="Independent, leaning Democrat"]=2/6
state_threat_white_change2$PID_post[state_threat_white_change2$post_pid=="Independent"]=3/6
state_threat_white_change2$PID_post[state_threat_white_change2$post_pid=="Independent, leaning Republican"]=4/6
state_threat_white_change2$PID_post[state_threat_white_change2$post_pid=="Not so strong Republican"]=5/6
state_threat_white_change2$PID_post[state_threat_white_change2$post_pid=="Strong Republican"]=1

state_threat_white_change2$GENDER[state_threat_white_change2$gender=="Male"]=0
state_threat_white_change2$GENDER[state_threat_white_change2$gender=="Female"]=1

state_threat_white_change2$IDEO_pre[state_threat_white_change2$ideo=="Extremely liberal"]=0
state_threat_white_change2$IDEO_pre[state_threat_white_change2$ideo=="Liberal"]=1/6
state_threat_white_change2$IDEO_pre[state_threat_white_change2$ideo=="Slightly liberal"]=2/6
state_threat_white_change2$IDEO_pre[state_threat_white_change2$ideo=="Moderate, middle of the road"]=3/6
state_threat_white_change2$IDEO_pre[state_threat_white_change2$ideo=="Slightly conservative"]=4/6
state_threat_white_change2$IDEO_pre[state_threat_white_change2$ideo=="Conservative"]=5/6
state_threat_white_change2$IDEO_pre[state_threat_white_change2$ideo=="Extremely conservative"]=1

state_threat_white_change2$INCOME[state_threat_white_change2$income=="Less than $10,000"]=0
state_threat_white_change2$INCOME[state_threat_white_change2$income=="$10,000 to $19,999"]=1/11
state_threat_white_change2$INCOME[state_threat_white_change2$income=="$20,000 to $29,999"]=2/11
state_threat_white_change2$INCOME[state_threat_white_change2$income=="$30,000 to $39,999"]=3/11
state_threat_white_change2$INCOME[state_threat_white_change2$income=="$40,000 to $49,999"]=4/11
state_threat_white_change2$INCOME[state_threat_white_change2$income=="$50,000 to $59,999"]=5/11
state_threat_white_change2$INCOME[state_threat_white_change2$income=="$60,000 to $69,999"]=6/11
state_threat_white_change2$INCOME[state_threat_white_change2$income=="$70,000 to $79,999"]=7/11
state_threat_white_change2$INCOME[state_threat_white_change2$income=="$80,000 to $89,999"]=8/11
state_threat_white_change2$INCOME[state_threat_white_change2$income=="$90,000 to $99,999"]=9/11
state_threat_white_change2$INCOME[state_threat_white_change2$income=="$100,000 to $149,999"]=10/11
state_threat_white_change2$INCOME[state_threat_white_change2$income=="$150,000 or more"]=1

state_threat_white_change2$EDU[state_threat_white_change2$edu=="Less than high school degree"]=0
state_threat_white_change2$EDU[state_threat_white_change2$edu=="High school graduate (high school diploma or equivalent including GED)"]=1/6
state_threat_white_change2$EDU[state_threat_white_change2$edu=="Some college but no degree"]=2/6
state_threat_white_change2$EDU[state_threat_white_change2$edu=="Associate degree in college (2-year)"]=3/6
state_threat_white_change2$EDU[state_threat_white_change2$edu=="Bachelor's degree in college (4-year)"]=4/6
state_threat_white_change2$EDU[state_threat_white_change2$edu=="Master's degree"]=5/6
state_threat_white_change2$EDU[state_threat_white_change2$edu=="Doctoral degree"]=1
state_threat_white_change2$EDU[state_threat_white_change2$edu=="Professional degree (JD, MD)"]=1



state_threat_white_change2$Affective_polarization = abs(state_threat_white_change2$feeling_therm_3 -state_threat_white_change2$feeling_therm_5)/100


state_threat_white_change2$ANTI_DEM_support_pre = (state_threat_white_change2$tradtional_disappearing_force_pre+
                                                     state_threat_white_change2$reasonable_violence_pre)/2
state_threat_white_change2$ANTI_DEM_support = (state_threat_white_change2$tradtional_disappearing_force_post+
                                                 state_threat_white_change2$reasonable_violence_post)/2

state_threat_white_change2$Democrat_feeling_therm = (state_threat_white_change2$feeling_therm_3)/100
state_threat_white_change2$Republican_feeling_therm = (state_threat_white_change2$feeling_therm_5)/100
state_threat_white_change2$White_feeling_therm = (state_threat_white_change2$feeling_therm_6)/100
state_threat_white_change2$Black_feeling_therm = (state_threat_white_change2$feeling_therm_7)/100

state_threat_white_change_white_only1$Blacks_work_without_special_favors[state_threat_white_change_white_only1$rr_scale_1=="Disagree Strongly"]=0
state_threat_white_change_white_only1$Blacks_work_without_special_favors[state_threat_white_change_white_only1$rr_scale_1=="Disagree"]=1/6
state_threat_white_change_white_only1$Blacks_work_without_special_favors[state_threat_white_change_white_only1$rr_scale_1=="Somewhat disagree"]=2/6
state_threat_white_change_white_only1$Blacks_work_without_special_favors[state_threat_white_change_white_only1$rr_scale_1=="Neither agree nor disagree"]=3/6
state_threat_white_change_white_only1$Blacks_work_without_special_favors[state_threat_white_change_white_only1$rr_scale_1=="Somewhat agree"]=4/6
state_threat_white_change_white_only1$Blacks_work_without_special_favors[state_threat_white_change_white_only1$rr_scale_1=="Agree"]=5/6
state_threat_white_change_white_only1$Blacks_work_without_special_favors[state_threat_white_change_white_only1$rr_scale_1=="Agree strongly"]=1

state_threat_white_change_white_only1$Black_have_gotten_LESS_than_they_deserve_REVERSE[state_threat_white_change_white_only1$rr_scale_2=="Disagree Strongly"]=1
state_threat_white_change_white_only1$Black_have_gotten_LESS_than_they_deserve_REVERSE[state_threat_white_change_white_only1$rr_scale_2=="Disagree"]=5/6
state_threat_white_change_white_only1$Black_have_gotten_LESS_than_they_deserve_REVERSE[state_threat_white_change_white_only1$rr_scale_2=="Somewhat disagree"]=4/6
state_threat_white_change_white_only1$Black_have_gotten_LESS_than_they_deserve_REVERSE[state_threat_white_change_white_only1$rr_scale_2=="Neither agree nor disagree"]=3/6
state_threat_white_change_white_only1$Black_have_gotten_LESS_than_they_deserve_REVERSE[state_threat_white_change_white_only1$rr_scale_2=="Somewhat agree"]=2/6
state_threat_white_change_white_only1$Black_have_gotten_LESS_than_they_deserve_REVERSE[state_threat_white_change_white_only1$rr_scale_2=="Agree"]=1/6
state_threat_white_change_white_only1$Black_have_gotten_LESS_than_they_deserve_REVERSE[state_threat_white_change_white_only1$rr_scale_2=="Agree strongly"]=0

state_threat_white_change_white_only1$generation_of_slavery_made_it_HARD_REVERSE[state_threat_white_change_white_only1$rr_scale_3=="Disagree Strongly"]=1
state_threat_white_change_white_only1$generation_of_slavery_made_it_HARD_REVERSE[state_threat_white_change_white_only1$rr_scale_3=="Disagree"]=5/6
state_threat_white_change_white_only1$generation_of_slavery_made_it_HARD_REVERSE[state_threat_white_change_white_only1$rr_scale_3=="Somewhat disagree"]=4/6
state_threat_white_change_white_only1$generation_of_slavery_made_it_HARD_REVERSE[state_threat_white_change_white_only1$rr_scale_3=="Neither agree nor disagree"]=3/6
state_threat_white_change_white_only1$generation_of_slavery_made_it_HARD_REVERSE[state_threat_white_change_white_only1$rr_scale_3=="Somewhat agree"]=2/6
state_threat_white_change_white_only1$generation_of_slavery_made_it_HARD_REVERSE[state_threat_white_change_white_only1$rr_scale_3=="Agree"]=1/6
state_threat_white_change_white_only1$generation_of_slavery_made_it_HARD_REVERSE[state_threat_white_change_white_only1$rr_scale_3=="Agree strongly"]=0

state_threat_white_change_white_only1$Black_not_trying_hard_enough[state_threat_white_change_white_only1$rr_scale_4=="Disagree Strongly"]=0
state_threat_white_change_white_only1$Black_not_trying_hard_enough[state_threat_white_change_white_only1$rr_scale_4=="Disagree"]=1/6
state_threat_white_change_white_only1$Black_not_trying_hard_enough[state_threat_white_change_white_only1$rr_scale_4=="Somewhat disagree"]=2/6
state_threat_white_change_white_only1$Black_not_trying_hard_enough[state_threat_white_change_white_only1$rr_scale_4=="Neither agree nor disagree"]=3/6
state_threat_white_change_white_only1$Black_not_trying_hard_enough[state_threat_white_change_white_only1$rr_scale_4=="Somewhat agree"]=4/6
state_threat_white_change_white_only1$Black_not_trying_hard_enough[state_threat_white_change_white_only1$rr_scale_4=="Agree"]=5/6
state_threat_white_change_white_only1$Black_not_trying_hard_enough[state_threat_white_change_white_only1$rr_scale_4=="Agree strongly"]=1

state_threat_white_change_white_only1$RR_scale=(
  state_threat_white_change_white_only1$Blacks_work_without_special_favors +
    state_threat_white_change_white_only1$Black_have_gotten_LESS_than_they_deserve_REVERSE +
    state_threat_white_change_white_only1$generation_of_slavery_made_it_HARD_REVERSE + 
    state_threat_white_change_white_only1$Black_not_trying_hard_enough)/4


state_threat_white_change_white_only1<- dplyr::select(filter(state_threat_white_change2, white == "Whites"), c(zip:Black_feeling_therm))


#########################################
#########################################
######## STUDY 4 cleaning
#########################################
#########################################
prime_corrections_white_fight <- read_tsv("~/Google Drive/My Drive/Fall_2023/ongoing_projects/white_fight/prime_corrections3_with_pop_change.tsv")

prime_corrections_white_fight$correct_poc[prime_corrections_white_fight$random==1]=0
prime_corrections_white_fight$correct_poc[prime_corrections_white_fight$random==2]=1

prime_corrections_white_fight$PID_pre[prime_corrections_white_fight$pid=="Strong Democrat"]=0
prime_corrections_white_fight$PID_pre[prime_corrections_white_fight$pid=="Not so strong Democrat"]=1/6
prime_corrections_white_fight$PID_pre[prime_corrections_white_fight$pid=="Independent, leaning Democrat"]=2/6
prime_corrections_white_fight$PID_pre[prime_corrections_white_fight$pid=="Independent"]=3/6
prime_corrections_white_fight$PID_pre[prime_corrections_white_fight$pid=="Independent, leaning Republican"]=4/6
prime_corrections_white_fight$PID_pre[prime_corrections_white_fight$pid=="Not so strong Republican"]=5/6
prime_corrections_white_fight$PID_pre[prime_corrections_white_fight$pid=="Strong Republican"]=1

prime_corrections_white_fight$IDEO_pre[prime_corrections_white_fight$ideo=="Extremely liberal"]=0
prime_corrections_white_fight$IDEO_pre[prime_corrections_white_fight$ideo=="Liberal"]=1/6
prime_corrections_white_fight$IDEO_pre[prime_corrections_white_fight$ideo=="Slightly liberal"]=2/6
prime_corrections_white_fight$IDEO_pre[prime_corrections_white_fight$ideo=="Moderate, middle of the road"]=3/6
prime_corrections_white_fight$IDEO_pre[prime_corrections_white_fight$ideo=="Slightly conservative"]=4/6
prime_corrections_white_fight$IDEO_pre[prime_corrections_white_fight$ideo=="Conservative"]=5/6
prime_corrections_white_fight$IDEO_pre[prime_corrections_white_fight$ideo=="Extremely conservative"]=1

prime_corrections_white_fight$Support_for_violence_pre[prime_corrections_white_fight$anti_dem_pretreat_1=="Strongly disagree"]=0
prime_corrections_white_fight$Support_for_violence_pre[prime_corrections_white_fight$anti_dem_pretreat_1=="Somewhat disagree"]=1/4
prime_corrections_white_fight$Support_for_violence_pre[prime_corrections_white_fight$anti_dem_pretreat_1=="Neither agree nor disagree"]=2/4
prime_corrections_white_fight$Support_for_violence_pre[prime_corrections_white_fight$anti_dem_pretreat_1=="Somewhat agree"]=3/4
prime_corrections_white_fight$Support_for_violence_pre[prime_corrections_white_fight$anti_dem_pretreat_1=="Strongly agree"]=1

prime_corrections_white_fight$Electoral_skepticism[prime_corrections_white_fight$`exit_poll_anti-dem_1`=="Strongly disagree"]=0
prime_corrections_white_fight$Electoral_skepticism[prime_corrections_white_fight$`exit_poll_anti-dem_1`=="Somewhat disagree"]=1/4
prime_corrections_white_fight$Electoral_skepticism[prime_corrections_white_fight$`exit_poll_anti-dem_1`=="Neither agree nor disagree"]=2/4
prime_corrections_white_fight$Electoral_skepticism[prime_corrections_white_fight$`exit_poll_anti-dem_1`=="Somewhat agree"]=3/4
prime_corrections_white_fight$Electoral_skepticism[prime_corrections_white_fight$`exit_poll_anti-dem_1`=="Strongly agree"]=1

prime_corrections_white_fight$Support_for_violence[prime_corrections_white_fight$anti_dem_general_1=="Strongly disagree"]=0
prime_corrections_white_fight$Support_for_violence[prime_corrections_white_fight$anti_dem_general_1=="Somewhat disagree"]=1/4
prime_corrections_white_fight$Support_for_violence[prime_corrections_white_fight$anti_dem_general_1=="Neither agree nor disagree"]=2/4
prime_corrections_white_fight$Support_for_violence[prime_corrections_white_fight$anti_dem_general_1=="Somewhat agree"]=3/4
prime_corrections_white_fight$Support_for_violence[prime_corrections_white_fight$anti_dem_general_1=="Strongly agree"]=1

prime_corrections_white_fight$INCOME[prime_corrections_white_fight$income=="Less than $10,000"]=0
prime_corrections_white_fight$INCOME[prime_corrections_white_fight$income=="$10,000 to $19,999"]=1/11
prime_corrections_white_fight$INCOME[prime_corrections_white_fight$income=="$20,000 to $29,999"]=2/11
prime_corrections_white_fight$INCOME[prime_corrections_white_fight$income=="$30,000 to $39,999"]=3/11
prime_corrections_white_fight$INCOME[prime_corrections_white_fight$income=="$40,000 to $49,999"]=4/11
prime_corrections_white_fight$INCOME[prime_corrections_white_fight$income=="$50,000 to $59,999"]=5/11
prime_corrections_white_fight$INCOME[prime_corrections_white_fight$income=="$60,000 to $69,999"]=6/11
prime_corrections_white_fight$INCOME[prime_corrections_white_fight$income=="$70,000 to $79,999"]=7/11
prime_corrections_white_fight$INCOME[prime_corrections_white_fight$income=="$80,000 to $89,999"]=8/11
prime_corrections_white_fight$INCOME[prime_corrections_white_fight$income=="$90,000 to $99,999"]=9/11
prime_corrections_white_fight$INCOME[prime_corrections_white_fight$income=="$100,000 to $149,999"]=10/11
prime_corrections_white_fight$INCOME[prime_corrections_white_fight$income=="$150,000 or more"]=1

prime_corrections_white_fight$EDU[prime_corrections_white_fight$edu=="Less than high school degree"]=0
prime_corrections_white_fight$EDU[prime_corrections_white_fight$edu=="High school graduate (high school diploma or equivalent including GED)"]=1/6
prime_corrections_white_fight$EDU[prime_corrections_white_fight$edu=="Some college but no degree"]=2/6
prime_corrections_white_fight$EDU[prime_corrections_white_fight$edu=="Associate degree in college (2-year)"]=3/6
prime_corrections_white_fight$EDU[prime_corrections_white_fight$edu=="Bachelor's degree in college (4-year)"]=4/6
prime_corrections_white_fight$EDU[prime_corrections_white_fight$edu=="Master's degree"]=5/6
prime_corrections_white_fight$EDU[prime_corrections_white_fight$edu=="Doctoral degree"]=1
prime_corrections_white_fight$EDU[prime_corrections_white_fight$edu=="Professional degree (JD, MD)"]=1

prime_corrections_white_fight_WHITE$HispanicΔ = (prime_corrections_white_fight_WHITE$county_change_in_pct_hispanic)
prime_corrections_white_fight_WHITE$Demo_Correction = (prime_corrections_white_fight_WHITE$correct_poc)

prime_corrections_white_fight_WHITE<- dplyr::select(filter(prime_corrections_white_fight, 
                                                           race == "White"), c(StartDate:Support_for_violence_pre))






##############################
##############################
###### Figure 3  #############
##############################
##############################

figure3.1_PNAS <- lm(Support_for_political_violence ~ Diversification  * WhiteΔ 
                     + PID + GENDER + EDU  + VOTER_REGISTERED_ALL, 
                     data = study1_PNAS_yougov_WHITE,
                     weights = weight)

summary(figure3.1_PNAS)
interact_plot(figure3.1_PNAS, 
              pred =WhiteΔ, 
              modx = Diversification, 
              interval = T ,
              x.label = "Change in White population percentage, county-level",
              y.label = "Support for Political Violence",
              main.title = "White Population Change",
              legend.main = "",
              modx.labels = c("No Demographic Change Prime", "National Demographic Change Prime"),
              color.class = "CUD Bright")

ss = sim_slopes(figure3.1_PNAS, 
                modx = WhiteΔ, 
                pred =Diversification, 
                modx.values = c(-0.1, -0.075, -0.05, -0.025, 0, 0.025, 0.05),
                robust = T,)
plot(ss) 
ss
as_huxtable(ss)
ss = sim_slopes(study2_yougov_white_county_change_unweighted, pred =diversification_treatment, modx = county_change_in_pct_white,
                modx.values = c(-0.1, -0.075, -0.05, -0.025, 0, 0.025, 0.05   ),)
plot(ss)
ss
johnson_neyman(study2_yougov_white_county_change_weighted, 
               pred =Diversification, 
               modx = County_Change_White, alpha = .05)

##############################
##############################
###### Figures 3-4 ###########
##############################
##############################

figure3.2_PNAS<- lm(Support_for_political_violence ~ 
                      Diversification  *BlackΔ
                    + PID + GENDER + EDU  + VOTER_REGISTERED_ALL, 
                    data = study1_PNAS_yougov_WHITE,
                    weights = weight)
summary(figure3.2_PNAS)
figure4_PNAS = sim_slopes(figure3.2_PNAS, 
                          pred =Diversification,
                          modx = BlackΔ,
                          modx.values = c(-0.1, -0.075, -0.05, -0.025, 0, 0.025, 0.05, 0.075, 0.1   ),)
plot(figure4_PNAS)
figure4_PNAS

##############################
##############################
###### Figure 5  #############
##############################
##############################

johnson_neyman(figure3.2_PNAS, 
               pred =Diversification, 
               modx = BlackΔ, alpha = .05)


##############################
##############################
###### Table 1  ##############
##############################
##############################

study1_black_county_change_RR<- lm(Racial_resentment_scale ~ diversification_treatment  * county_change_in_pct_black
                                   + PID + GENDER + EDU  + VOTER_REGISTERED_ALL, 
                                   data = study1_PNAS_yougov_WHITE,
                                   weights = weight)
summary(study1_black_county_change_RR)
table1_PNAS = sim_slopes(study1_black_county_change_RR, 
                         pred =diversification_treatment, 
                         modx = county_change_in_pct_black,
                         modx.values = c(-0.1, -0.075, -0.05, -0.025, 0, 0.025, 0.05, 0.075, 0.1 ))
plot(table1_PNAS)
table1_PNAS

##################
as_huxtable(table1_PNAS,sig.levels = c(`****` = 0.001, `***` = 0.01, `**` = 0.05, `*` = 0.1),)
##################

study1_white_county_change_RR <- lm(Racial_resentment_scale ~ diversification_treatment  *county_change_in_pct_white
                                    + PID + GENDER + EDU  + VOTER_REGISTERED_ALL, 
                                    data = study1_PNAS_yougov_WHITE,
                                    weights = weight)
summary(study1_white_county_change_RR)
study1_hispanic_county_change_RR<- lm(Racial_resentment_scale ~ diversification_treatment  *county_change_in_pct_hispanic
                                      + PID + GENDER + EDU  + VOTER_REGISTERED_ALL, 
                                      data = study1_PNAS_yougov_WHITE,
                                      weights = weight)
summary(study1_hispanic_county_change_RR)

study1_asian_county_change_RR<- lm(Racial_resentment_scale ~ diversification_treatment  *county_change_in_pct_asian
                                   + PID + GENDER + EDU  + VOTER_REGISTERED_ALL, 
                                   data = study1_PNAS_yougov_WHITE,
                                   weights = weight)
summary(study1_asian_county_change_RR)

##############################
##############################
###### Tables 2 and 3 ########
##############################
##############################

cor.test(study1_PNAS_yougov_WHITE$county_change_in_pct_black,
         study1_PNAS_yougov_WHITE$county_change_in_pct_white)
cor.test(study1_PNAS_yougov_WHITE$county_change_in_pct_asian,
         study1_PNAS_yougov_WHITE$county_change_in_pct_white)
cor.test(study1_PNAS_yougov_WHITE$county_change_in_pct_hispanic,
         study1_PNAS_yougov_WHITE$county_change_in_pct_white)

# BLACK CORRELATIONS
cor.test(study1_PNAS_yougov_WHITE$county_change_in_pct_hispanic,
         study1_PNAS_yougov_WHITE$county_change_in_pct_black)
cor.test(study1_PNAS_yougov_WHITE$county_change_in_pct_asian,
         study1_PNAS_yougov_WHITE$county_change_in_pct_black)
cor.test(study1_PNAS_yougov_WHITE$county_change_in_pct_black,
         study1_PNAS_yougov_WHITE$county_change_in_pct_white)

########## ???

yougov_hispanic_county_change_unweighted<- lm(Support_for_political_violence ~ 
                                                       Diversification  *county_change_in_pct_hispanic
                                                     + PID + GENDER + EDU  + VOTER_REGISTERED_ALL, 
                                                     data = study1_PNAS_yougov_WHITE,
                                                     weights = weight)
summary(yougov_hispanic_county_change_unweighted)
interact_plot(yougov_hispanic_county_change_unweighted, pred =county_change_in_pct_hispanic, modx = diversification_treatment, interval = T ,
              x.label = "Change in Hispanic population percentage, county-level",
              y.label = "White Fight Sentiment",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),)

yougov_hispanic_county_change_unweighted<- lm(Support_for_political_violence ~ 
                  Diversification  *county_change_in_pct_asian
                + PID + GENDER + EDU  + VOTER_REGISTERED_ALL, 
                data = study1_PNAS_yougov_WHITE)
summary(yougov_hispanic_county_change_unweighted)
interact_plot(yougov_hispanic_county_change_unweighted, 
              pred =county_change_in_pct_asian, 
              modx = Diversification, interval = T ,
              x.label = "Change in Asian population percentage, county-level",
              y.label = "White Fight Sentiment",
              legend.main = "",
              main.title = "",
              modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"))

stargazer(study2_yougov_black_county_change_unweighted,
          study2_yougov_white_county_change_unweighted,
          study2_yougov_hispanic_county_change_unweighted,
          study2_yougov_asian_county_change_unweighted,
          type = "html")
stargazer(study2_yougov_white_county_change_weighted,
          study2_yougov_black_county_change_weighted,
          type = "html")
stargazer(reg_vote.4,
          study2_yougov_hispanic_county_change_weighted,
          type = "html")




##############################
##############################
###### FIGURE 6 ##############
##############################
##############################
threat_national_level_black<- lm(national_demographic_change_post ~ diversification_prime*county_change_in_pct_black
                                 + EDU + INCOME + PID + IDEO_pre  + national_demographic_change
                                 , data = study2_PNAS_LUCID_white)
summary(threat_national_level_black)
ss = sim_slopes(threat_national_level_black, pred =diversification_prime, modx = county_change_in_pct_black,
                modx.values = c(-0.1, -0.075, -0.05, -0.025, 0, 0.025, 0.05, 0.075, 0.1 ))
plot(ss)
ss
plot_black<- interact_plot(threat_national_level_black, pred =county_change_in_pct_black, modx = diversification_prime, interval = T ,
                           x.label = "Change in Black population percentage, county-level",
                           y.label = "Fearful about National Demographic Change",
                           legend.main = "",
                           main.title = "",
                           modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
                           colors = "CUD Bright")

threat_national_level_white<- lm(national_demographic_change_post ~ diversification_prime*county_change_in_pct_white
                                 + EDU + INCOME + PID + IDEO_pre  + national_demographic_change
                                 , data = study2_PNAS_LUCID_white)
summary(threat_national_level_white)
plot_white<- interact_plot(threat_national_level_white, pred =county_change_in_pct_white, modx = diversification_prime, interval = T ,
                           x.label = "Change in White population percentage, county-level",
                           y.label = "Fearful about National Demographic Change",
                           legend.main = "",
                           main.title = "",
                           modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
                           colors = "CUD Bright")
plot_white

ss = sim_slopes(threat_national_level_white, pred =diversification_prime, modx = county_change_in_pct_white,
                modx.values = c(-0.1, -0.075, -0.05, -0.025, 0, 0.025, 0.05, 0.075, 0.1 ))
plot(ss)
ss
p2 = grid.arrange(plot_black, plot_white)
ggsave("Figure 6, higher quality.pdf", plot = p2,
       width = 10, height = 12, units = "in", dpi = 1200)

##############################
##############################
###### FIGURE 7 ##############
##############################
##############################
PNAS_Figure7<- lm(PID_post ~ diversification_prime*county_change_in_pct_black  
                  + EDU + INCOME + PID 
                  , data = study2_PNAS_LUCID_white)
summary(PNAS_Figure7)
Figure7_PNAS = interact_plot(PNAS_Figure7, 
                             pred =county_change_in_pct_black, 
                             modx = diversification_prime, 
                             interval = T ,
                             x.label = "Change in Black population percentage, county-level",
                             y.label = "Party Identification, (Democrat is 0, Republican is 1)",
                             legend.main = "",
                             main.title = "",
                             modx.labels = c("No Demographic Change Prime", "Demographic Change Prime"),
                             colors = "CUD Bright")
Figure7_PNAS
ggsave("Figure 7, higher quality.pdf", plot = p3,
       width = 10, height = 8, units = "in", dpi = 1200)
ss = sim_slopes(PNAS_Figure7, pred =diversification_prime, modx = county_change_in_pct_black,
                modx.values = c(-0.1, -0.075, -0.05, -0.025, 0, 0.025, 0.05, 0.075, 0.1 ))
plot(ss)
ss

##############################
##############################
###### FIGURE 8 ##############
##############################
##############################
figure8.1_PNAS <- lm(ANTI_DEM_support ~ national_VS_state_threat*county_change_in_pct_white
                     + ANTI_DEM_support_pre + PID_pre + EDU + INCOME, 
                     data = state_threat_white_change_white_only1 )
summary(figure8.1_PNAS)
state_prime1= interact_plot(figure8.1_PNAS, pred =county_change_in_pct_white, modx = national_VS_state_threat, interval = T,
                            x.label = "Change in White population percentage, county-level",
                            y.label = "Support for Political Violence",
                            modx.labels = c("State Demographic Change Prime", "National Demographic Change Prime"),
                            legend.main = "",
                            colors = "CUD Bright")
figure8.2_PNAS <- lm(ANTI_DEM_support ~ national_VS_state_threat*county_change_in_pct_black
                     + ANTI_DEM_support_pre + PID_pre + EDU + INCOME, 
                     data = state_threat_white_change_white_only1 )
summary(figure8.2_PNAS)
state_prime2 <-interact_plot(figure8.2_PNAS, pred =county_change_in_pct_black, modx = national_VS_state_threat, interval = T,
                             x.label = "Change in Black population percentage, county-level",
                             y.label = "",
                             modx.labels = c("State Demographic Change Prime", "National Demographic Change Prime"),
                             legend.main = "",
                             colors = "CUD Bright")
figure8.3_PNAS <- lm(ANTI_DEM_support ~ national_VS_state_threat*county_change_in_pct_hispanic
                     + ANTI_DEM_support_pre + PID_pre + EDU + INCOME, 
                     data = state_threat_white_change_white_only1)
summary(figure8.3_PNAS)
state_prime3 <- interact_plot(figure8.3_PNAS, pred =county_change_in_pct_hispanic, modx = national_VS_state_threat, interval = T,
                              x.label = "Change in Hispanic population percentage, county-level",
                              y.label = "Support for Political Violence",
                              modx.labels = c("State Demographic Change Prime", "National Demographic Change Prime"),
                              legend.main = "",
                              colors = "CUD Bright")
figure8.4_PNAS <- lm(ANTI_DEM_support ~ national_VS_state_threat*county_change_in_pct_asian
                     + ANTI_DEM_support_pre + PID_pre + EDU + INCOME, 
                     data = state_threat_white_change_white_only1 )
summary(figure8.4_PNAS)
state_prime4 <- interact_plot(figure8.4_PNAS, pred =county_change_in_pct_asian, modx = national_VS_state_threat, interval = T,
                              x.label = "Change in Asian population percentage, county-level",
                              y.label = "",
                              modx.labels = c("State Demographic Change Prime", "National Demographic Change Prime"),
                              legend.main = "",
                              colors = "CUD Bright")
p_s1 = grid.arrange(state_prime1,
                    state_prime2,
                    state_prime3,
                    state_prime4)
ggsave("Figure 8, Higher quality.jpeg", plot = p_s1,
       width = 15, height = 12, units = "in", dpi = 1200)

######################################
######################################
####### FIGURE 9 #####################
######################################
######################################

Figure9_PNAS= lm(Support_for_violence ~ 
                   Demo_Correction *HispanicΔ +
                   Support_for_violence_pre +
                   PID_pre + EDU + INCOME,
                 data = prime_corrections_white_fight_WHITE )
summary(Figure9_PNAS)
ss = sim_slopes(Figure9_PNAS, 
                pred =Demo_Correction, 
                modx = HispanicΔ,
                modx.values = c(-0.15,-0.125,-0.1, -0.075, -0.05, -0.025, 0, 0.025, 0.05, 0.075, 0.1, .125 ))
plot_hispanic_slope= plot(ss)
ss
plot_hispanic_interaction <- interact_plot(Figure9_PNAS, 
                                           pred =HispanicΔ, 
                                           modx = Demo_Correction, 
                                           interval = T ,
                                           x.label = "Hispanic County Δ",
                                           y.label = "Support for Political Violence",
                                           legend.main = "",
                                           main.title = "",
                                           modx.labels = c("Demo Prime", "Demo Prime + Correction"),
                                           colors = "CUD Bright",)
figure9_pnas = grid.arrange(plot_hispanic_interaction, plot_hispanic_slope )
ggsave("Figure 9, higher quality.jpeg", 
       plot = figure9_pnas,
       width = 10, 
       height = 8, units = "in", dpi = 1200)
Figure9_black_change_PNAS= lm(Support_for_violence ~ correct_poc *county_change_in_pct_black +
                 Support_for_violence_pre +
                 PID_pre + EDU + INCOME,
               data = prime_corrections_white_fight_WHITE)
summary(Figure9_black_change_PNAS)
ss = sim_slopes(Figure9_black_change_PNAS, 
                pred =correct_poc, 
                modx = county_change_in_pct_black,
                modx.values = c(-0.1, -0.075, -0.05, -0.025, 0, 0.025, 0.05, 0.075, 0.1 ))
plot(ss)
ss
interact_plot(ss, pred =county_change_in_pct_black, modx = correct_poc, interval = T ,
              x.label = "Party Identification, (Strong Democrat is 0, Strong Republican is 1)",
              y.label = "",
              legend.main = "",
              main.title = "",
              modx.labels = c("Demo Prime", "Demo Prime + Correction"),
              colors = "CUD Bright",
              linearity.check = T)

Figure9_white_change_PNAS= lm(Support_for_violence ~ correct_poc *county_change_in_pct_white +
                 Support_for_violence_pre  +
                 PID_pre + EDU + INCOME,
               data = prime_corrections_white_fight_WHITE )
summary(Figure9_white_change_PNAS)
ss = sim_slopes(Figure9_white_change_PNAS, pred =correct_poc, modx = county_change_in_pct_white,
                modx.values = c(-0.15,-0.1, -0.075, -0.05, -0.025, 0, 0.025, 0.05, 0.075, 0.1, 0.15 ))
plot(ss)
ss
Figure9_asian_change_PNAS= lm(Support_for_violence ~ correct_poc *county_change_in_pct_asian +
                 Support_for_violence_pre +
                 PID_pre + EDU + INCOME,
               data = prime_corrections_white_fight_WHITE )
summary(Figure9_asian_change_PNAS)
ss = sim_slopes(Figure9_asian_change_PNAS, 
                pred =correct_poc, 
                modx = county_change_in_pct_asian,
                modx.values = c(-0.1, -0.075, -0.05, -0.025, 0, 0.025, 0.05, 0.075, 0.1 ))
plot(ss)
ss


